﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyCommon.WebControls
{
    /// <summary>
    /// 接收可空Boolean值，显示相应的中文映射
    /// </summary>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:BooleanLiteral runat=server></{0}:BooleanLiteral>")]
    public class BooleanLiteral : Literal
    {
        /// <summary>
        /// true对应的中文映射，默认为“是”
        /// </summary>
        [Category("Appearance")]
        [DefaultValue("是")]
        [Localizable(true)]
        public string TrueText
        {
            get
            {
                String s = (String)ViewState["TrueText"];
                return ((s == null) ? "是" : s);
            }

            set
            {
                ViewState["TrueText"] = value;
            }
        }
        /// <summary>
        /// false对应的中文映射，默认为“否”
        /// </summary>
        [Category("Appearance")]
        [DefaultValue("否")]
        [Localizable(true)]
        public string FalseText
        {
            get
            {
                String s = (String)ViewState["FalseText"];
                return ((s == null) ? "否" : s);
            }

            set
            {
                ViewState["FalseText"] = value;
            }
        }

        /// <summary>
        /// null对应的中文映射，默认为空字符
        /// </summary>
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string NullText
        {
            get
            {
                String s = (String)ViewState["NullText"];
                return ((s == null) ? "" : s);
            }

            set
            {
                ViewState["NullText"] = value;
            }
        }
        /// <summary>
        /// 接受的Boolean值
        /// </summary>
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool? BooleanValue
        {
            get
            {
                if (ViewState["BooleanValue"] == null)
                {
                    ViewState["BooleanValue"] = false;
                }
                return ((bool)ViewState["BooleanValue"]);
            }

            set
            {
                ViewState["BooleanValue"] = value;
                if (value.HasValue)
                {
                    Text = value.Value ? TrueText : FalseText;
                }
                else
                {
                    Text = NullText;
                }
            }
        }

    }
}
